package com.biglead.demo.rabbit.order.delay;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 插件配置类
 * 需要使用 rabbitmq_delayed_message_exchange 插件
 */
@Configuration
public class OrderDelayedMessageConfig {

    public static final String DIRECT_QUEUE = "order.queue.direct";//队列
    public static final String DELAYED_EXCHANGE = "order.exchange.delayed";//延迟交换机
    public static final String ROUTING_KEY = "order.routingkey.bind";//绑定的routing-key


    /**
     * 定义队列
     **/
    @Bean
    public Queue orderDelayDirectQueue() {
        return new Queue(DIRECT_QUEUE, true);
    }

    /**
     * 定义延迟交换机
     * args:根据该参数进行灵活路由，设置为“direct”，意味着该插件具有与直连交换机具有相同的路由行为
     * 交换机类型为 x-delayed-message
     **/
    @Bean
    public CustomExchange orderDelayedExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }


    /**
     * 队列和延迟交换机绑定
     **/
    @Bean
    public Binding orderDelayedBinding() {
        return BindingBuilder
                .bind(orderDelayDirectQueue())
                .to(orderDelayedExchange())
                .with(ROUTING_KEY).noargs();
    }

}